$Import('app.common.getElement');

STK.register('app.common.menuEvent',function($){
	/*
	 * spec = {
			region : 
			tag : 
			over :
			delay :
		}
	 */
	return function(spec){
		var delay = spec.delay || 200;
		var region = spec.region;
		var tag = spec.tag;
		var timeout = null;
		var selectNode = null;
		var nodetype = spec.nodetype;
		var hover = function(e){
			if (timeout) {
				clearTimeout(timeout);
				timeout = null;
			}
			var el = $.core.evt.fixEvent(e).target;
			if (el.nodeName !== tag) {
				el = $.app.common.getElement(el, "parentNode", tag);
			}
			if(el && el.getAttribute("node-type") != nodetype) return;
			if (selectNode) {
				selectNode.className = "";
				selectNode = null;
			}
			if (el) {
				selectNode = el;
				spec['over'] && spec['over'](el);
			}
		}

		var out = function(e){
			timeout = setTimeout(function () {
				if (selectNode) {
					selectNode.className = "";
					selectNode = null;
				}
			}, delay);
		}
				
		$.addEvent(region, 'mouseover', hover);
		$.addEvent(region, 'mouseout', out);
		
		var that = {};
		that.destroy = function() {
			$.removeEvent(region, 'mouseover', hover);
			$.removeEvent(region, 'mouseout', out);
		};
		return that;
	};
});